import cv2

if __name__ == "__main__":
    face_cascade = cv2.CascadeClassifier("./cascades/haarcascade_frontalface_default.xml")
    eye_cascade = cv2.CascadeClassifier("./cascades/haarcascade_eye.xml")
    camera = cv2.VideoCapture(0)
    count = 0
    while True:
        ret, frame = camera.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(image=gray, scaleFactor=1.3, minNeighbors=5)
        for (x, y, w, h) in faces:
            img = cv2.rectangle(img=frame, pt1=(x, y), pt2=(x+w, y+h), color=(255, 0, 0))
            f = cv2.resize(gray[y:y+h, x:x+w], (200, 200))
            cv2.imwrite('./data/zhan/%s.jpg' % str(count), f)
            count += 1
        cv2.imshow("camera", frame)
        if cv2.waitKey(1000//12) == ord("q"):
            break

    camera.release()
    cv2.destroyAllWindows()